<!DOCTYPE html>
<html class="x-admin-sm">
    <head>
        <meta charset="UTF-8">
        <title>欢迎页面-X-admin2.2</title>
        <meta name="renderer" content="webkit">
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
        <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
        <link rel="stylesheet" href="../../static/css/font.css">
        <link rel="stylesheet" href="../../static/css/xadmin.css">
        <link rel="stylesheet" href="../../static/css/main.css">
        <script src="../../static/lib/layui/layui.js" charset="utf-8"></script>
        <script type="text/javascript" src="../../static/js/xadmin.js"></script>
        <!--[if lt IE 9]>
          <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
          <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
        <![endif]-->
    </head>
    <body>
        <div class="x-nav">
          <span class="layui-breadcrumb">
            <a href="#">首页</a>
            <a href="#">服务器管理</a>
            <a>
              <cite>服务器列表</cite></a>
          </span>
          <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right" onclick="location.reload()" title="刷新">
            <i class="layui-icon layui-icon-refresh" style="line-height:30px"></i></a>
        </div>
        <div class="layui-fluid">
            <div class="layui-row layui-col-space15">
                <div class="layui-col-md12">
                    <div class="layui-card">
                        <div class="layui-card-body ">
                            <form class="layui-form layui-col-space5">
                                <div class="layui-input-inline layui-show-xs-block">
                                    <select name="account" lay-filter="account" id="account" required="" lay-verify="required">
                                    </select>
                                </div>
                                <div class="layui-input-inline layui-show-xs-block">
                                    <select name="vpc" lay-filter="vpc" id="vpc" required="" lay-verify="required">
                                    </select>
                                </div>
                                <div class="layui-inline layui-show-xs-block">
                                    <input type="text" name="servername"  id="servername" placeholder="关键词搜索。。。" autocomplete="off" class="layui-input">
                                </div>
                                <div class="layui-inline layui-show-xs-block">
                                    <button class="layui-btn"  lay-submit="" lay-filter="search"><i class="layui-icon">&#xe615;</i></button>
                                </div>
                                <div class="layui-inline layui-show-xs-block">
                                    <button class="layui-btn"  lay-submit="" lay-filter="add"><i class="layui-icon"></i>添加服务器</button>
                                </div>
                            </form>
                        </div>

                        <div class="layui-card-body " id="server" lay-filter="server" lay-data="{id: 'server'}">

                        </div>
                    </div>
                </div>
            </div>
        </div> 
    </body>
    <script>
      layui.use(['table', 'form', 'layer'], function() {
          let table = layui.table, layer =layui.layer;
          let form = layui.form;

          form.verify({
            required:function (value,item) {
                let msg="必填项不能为空";
                value= $.trim(value);
                let isEmpty=!value || value.length<1;
               // 当前验证元素是select且为空时,将页面定位至layui渲染的select处，或自定义想定位的位置
                if(item.tagName=='SELECT' && isEmpty){
                    $("html").animate({
                        scrollTop:$(item).siblings(".layui-form-select").offset().top-74
                    },50);
                }
                if(isEmpty){
                    return msg;
                }
            }
          });
          $.ajax({
                  type: 'post',
                  url: '/AwsAccount/',
                  dataType: 'json',
                  success: function(res){
                      let data = res.data;
                      $('#account').append('<option value="" selected="true" disabled="disabled">---请选择账号---</option>');
                      $.each(data, function (index, item) {
                        $('#account').append('<option value="'+item.name+'">'+item.name+'</option>')
                      });
                      form.render();
                  }
                });
          form.on('select(account)', function (data) {
              var index = layer.load(1);
              $.ajax({
                  type: 'post',
                  url: '/Vpc/',
                  data: {'account': $('#account').val()},
                  dataType: 'json',
                  success: function(res){
                      layer.close(index);
                      let data = res.data;
                      $('#vpc').empty();
                      $('#vpc').append('<option value="" selected="true" disabled="disabled">---请选择vpc---</option>');
                      $.each(data, function (index, item) {
                        $('#vpc').append('<option value="'+item.vpc_id+'">'+item.name+'</option>')
                      });
                      form.render();
                  }
                });
          });

          form.on('submit(search)', function (data) {
            table.reload('server',{
                url: '/Server/',
                where: {account: $('#account').val(), vpc: $('#vpc').val(), servername: $('#servername').val()},
                page: {
                    curr: 1
                },
                loading: false
            });
            return false;
          });

          form.on('submit(add)', function (data){
              let htmlData = '        <div class="layui-fluid">\n' +
                  '            <div class="layui-row">\n' +
                  '                <form class="layui-form">\n' +
                  '                    <div class="layui-form-item">\n' +
                  '                      <label for="name" class="layui-form-label">\n' +
                  '                          <span class="x-red">*</span>实例名称\n' +
                  '                      </label>\n' +
                  '                      <div class="layui-input-inline">\n' +
                  '                          <input type="text" id="name" name="name" required="" lay-verify="required"\n' +
                  '                          autocomplete="off" class="layui-input">\n' +
                  '                      </div>\n' +
                  '                    </div>\n' +
                  '                    <div class="layui-form-item">\n' +
                  '                      <label for="name" class="layui-form-label">\n' +
                  '                          <span class="x-red">*</span>实例类型\n' +
                  '                      </label>\n' +
                  '                      <div class="layui-input-inline">\n' +
                  '                          <input type="text" id="instance_type" name="instance_type" required="" lay-verify="required"\n' +
                  '                          autocomplete="off" class="layui-input">\n' +
                  '                      </div>\n' +
                  '                    </div>\n' +
                  '                    <div class="layui-form-item">\n' +
                  '                      <label for="name" class="layui-form-label">\n' +
                  '                          <span class="x-red">*</span>硬盘大小(GB)\n' +
                  '                      </label>\n' +
                  '                      <div class="layui-input-inline">\n' +
                  '                          <input type="text" id="disk" name="disk" required="" lay-verify="required"\n' +
                  '                          autocomplete="off" class="layui-input">\n' +
                  '                      </div>\n' +
                  '                    </div>\n' +
                  '                    <div class="layui-form-item">\n' +
                  '                      <label for="name" class="layui-form-label">\n' +
                  '                          <span class="x-red">*</span>AMI\n' +
                  '                      </label>\n' +
                  '                      <div class="layui-input-inline">\n' +
                  '                        <select name="image_id" id="image_id" required="" lay-verify="required">\n' +
                  '                            <option value="ami-045f38c93733dd48d">Centos 7</option>\n' +
                  '                            <option value="ami-0c0f945615e3d22a3">Ubuntu 18.04</option>\n' +
                  '                        </select>\n' +
                  '                      </div>\n' +
                  '                    </div>\n' +
                  '                    <div class="layui-form-item">\n' +
                  '                      <label for="name" class="layui-form-label">\n' +
                  '                          <span class="x-red">*</span>Subnet\n' +
                  '                      </label>\n' +
                  '                      <div class="layui-input-inline">\n' +
                  '                        <select name="subnet" id="subnet" required="" lay-verify="required">\n' +
                  '                        </select>\n' +
                  '                      </div>\n' +
                  '                    </div>\n' +
                  '                    <div class="layui-form-item">\n' +
                  '                      <label for="name" class="layui-form-label">\n' +
                  '                          <span class="x-red">*</span>密钥对\n' +
                  '                      </label>\n' +
                  '                      <div class="layui-input-inline">\n' +
                  '                          <input type="text" id="key_name" name="key_name" required="" lay-verify="required"\n' +
                  '                          autocomplete="off" class="layui-input">\n' +
                  '                      </div>\n' +
                  '                    </div>\n' +
                  '                    <div class="layui-form-item">\n' +
                  '                      <label for="name" class="layui-form-label">\n' +
                  '                          <span class="x-red">*</span>数量\n' +
                  '                      </label>\n' +
                  '                      <div class="layui-input-inline">\n' +
                  '                          <input type="text" id="count" name="count" required="" lay-verify="required"\n' +
                  '                          autocomplete="off" class="layui-input">\n' +
                  '                      </div>\n' +
                  '                    </div>\n' +
                  '              </form>\n' +
                  '            </div>\n' +
                  '        </div>';
              var index = layer.open({
                  title: "添加实例",
                  area: ["400px", "500px"],
                  type: 1,
                  content: htmlData,//把数据显示在弹出层上
                  btn: ['确定', '我再想想'],
                  success: function (layero, index) {
                      var index1 = layer.load(1);
                      $.ajax({
                          type: 'post',
                          url: '/Subnet/',
                          data: {'account': $('#account').val(), 'vpc': $('#vpc').val()},
                          dataType: 'json',
                          success: function(res){
                              layer.close(index1);
                              let data = res.data;
                              $('#subnet').empty();
                              $.each(data, function (index, item) {
                                $('#subnet').append('<option value="'+item.subnet_id+'">'+item.avail_zone+'</option>')
                              });
                              form.render('select');
                          }
                      });


                  },
                  yes: function (index, layero) {//改完，点击确定
                      let account = $('#account').val();
                      let vpc = $('#vpc').val();
                      let subnet = $('#subnet').val();
                      let name = layero.find("#name").val();//获得修改后的值
                      let instance_type = layero.find("#instance_type").val();
                      let disk = layero.find("#disk").val();
                      let image_id = layero.find("#image_id").val();
                      let key_name = layero.find("#key_name").val();
                      let count = layero.find("#count").val();

                      var index2 = layer.load(1);
                      //同步更新缓存对应的值
                      $.post(
                          "/ServerAdd/",
                          {account: account, vpc: vpc, subnet: subnet, name: name, instance_type: instance_type,
                              disk: disk,image_id: image_id, key_name: key_name, count: count},
                          function (res) {

                              if (res.code === 0) {
                                  layer.msg(res.message, {time: 2000});
                                  layer.close(index);
                              } else {
                                  layer.alert(res.message);
                              }
                              layer.close(index2);


                          },
                          'json'
                      );
                  }
              });
              return false;
          });
          table.render({
              elem: '#server'
              , height: 500
              , url: ''
              , method: 'post'  //
              , page: true //开启分页
              , cols: [[ //表头
                  {field: 'id', title: 'id'}
                  , {field: 'name', title: '服务器名称'}
                  , {field: 'instance_type', title: '实例类型'}
                  , {field: 'private_address', title: '私有IP地址'}
                  , {field: 'public_address', title: '公网IP'}
                  , {field: 'zone', title: '区域'}
                  , {field: 'key_name', title: '密钥对'}
                  , {field: 'security_group', title: '安全组'}
                  , {field: 'status', title: '状态'}
                  , {title: '操作', align: 'center', toolbar: '#bar'}
              ]]
              , loading: true
          });


          table.on('tool(server)', function (obj) { //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
              let data = obj.data; //获得当前行数据
              let layEvent = obj.event; //获得 lay-event 对应的值
              let tr = obj.tr; //获得当前行 tr 的DOM对象

              if (layEvent === 'edit') { //编辑
                  //do something
                  let htmlData = '<div class="layui-fluid">\n' +
                  '            <div class="layui-row">\n' +
                  '                <form class="layui-form">\n' +
                  '                  <div class="layui-form-item">\n' +
                  '                    <div class="layui-block">\n' +
                  '                      <label class="layui-form-label">安全组</label>'+
                  '                      <div class="layui-input-inline">\n' +
                  '                        <select name="security_group" id="security_group" required="" lay-verify="required" multiple lay-filter="security_group" lay-search  lay-omit>\n' +
                  '                        </select>\n' +
                  '                      </div>\n' +
                  '                    </div>\n' +
                  '                  </div>' +
                  '              </form>\n' +
                  '            <div/>\n' +
                  '        </div>';
                  layer.open({
                      title: "更新",
                      area: ["400px", "400px"],
                      type: 1,
                      content: htmlData,//把数据显示在弹出层上
                      btn: ['确定', '我再想想'],
                      success: function (layero, index) {
                          var index1 = layer.load(1);
                          $.ajax({
                              type: 'post',
                              url: '/SecurityGroup/',
                              data: {'account': $('#account').val(), 'to_limit': 0, 'vpc': $('#vpc').val()},
                              dataType: 'json',
                              success: function(res){
                                  layer.close(index1);
                                  let data = res.data;
                                  let selected_list = obj.data.security_group.split(',')
                                  $('#security_group').empty();
                                  $('#security_group').append('<option value="">请选择安全组</option>')
                                  $.each(data, function (index, item) {
                                      if (selected_list.indexOf(item.name) > -1) {
                                          $('#security_group').append('<option selected value="'+item.group_id+'">'+item.name+'</option>')
                                      } else {
                                          $('#security_group').append('<option value="'+item.group_id+'">'+item.name+'</option>')
                                      }

                                  });
                                  form.render();
                              }
                          });
                      },
                      yes: function (index, layero) {//改完，点击确定
                          //同步更新缓存对应的值
                          let index1 = layer.load(1);
                          $.post(
                              "/ServerUpdate/",
                              {account: $('#account').val(), instance_id: data.id, security_group_list: $('#security_group').val().join(",")},
                              function (res) {
                                  layer.close(index1);
                                  let str = "";
                                  $('#security_group option:selected').each(function () {
                                            str += $(this).text() + ",";
                                  });
                                  layer.msg(res.message, {time: 2000});
                                  if (res.code === 0) {

                                      obj.update({
                                          security_group: str
                                      });
                                      layer.close(index);
                                  }
                              },
                              'json'
                          );

                      }
                  });
              }
          });
      });

    </script>
    <script type="text/html" id="bar">
        <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>

    </script>
</html>